********************************************************************************
* Paper： The Technical Default Spread, by Emilio Bisetti, Kai Li, and Jun Yu
* This file is used to produce the emprical results in the main paper
* Authors: Emilio Bisetti, Kai Li, and Jun Yu
* Date: 2024, May 14
********************************************************************************
clear all
set more off

// cd "...\TDS code sharing" * set the path to your folder

// You may need to install following STATA command
// ssc install asreg 
// ssc install reghdfe
// ssc install fcollapse


********************************************************************************
* Table 3: Panel A: strictness and portfolio level return 
*          Panel B: please run Table 1, and Table 2 Column 3 calcualte_annual_moments_with_dynare_simulation.m file
********************************************************************************
use Pseudodata/5port_firm_characteristics, clear

keep strict_5r exret
* Full sample summary statistics ***********************************************
matrix panel_b = J(2, 5,.)

// matrix  colnames panel_b = "1" "2" "3" "4" "5" "4-5" "5-1"
matrix  rownames panel_b =  "Strictness (pp)" "Excess Return (pp)"   

forvalue i = 1/5{

  mat panel_b[1,`i'] = strict_5r[`i']
  mat panel_b[2,`i'] = exret[`i']
}

matrix  colnames panel_b = "Low" "2" "3" "4" "High"  
mat list panel_b

* output table
esttab matrix(panel_b, fmt(2 2 2 2 2)), nomtitles ///
compress replace

esttab matrix(panel_b, fmt(2 2 2 2 2)) using Output/table3_panelA.tex, nomtitles booktabs replace fragment


********************************************************************************
* Table 4： Summary statistics for all variavles used in the paper 
********************************************************************************

use Pseudodata/ccm41a,clear


//cd "G:\Dropbox\Technical default project\Old version with redudent variables"
// For Fama-MacBeth regressions
//use cleandata/ccm41a_alt,clear

* variables expressed in percentage points in the paper
foreach var of varlist strict_5r p_failure edf roe roa {
	replace `var' = `var'*100 // nim in basis points
}

						   
replace gp = gp*100
replace ia = ia*100
replace cf_asset = cf_asset*100
replace profitability = profitability*100
replace reversal = reversal*100

* Full sample summary statistics ***********************************************
matrix panel_a = J(20, 6,.)

matrix  colnames panel_a = Mean "SD" p25 p50 p75 Observations
matrix  rownames panel_a = "Strictness\;(pp)"  "Pr(Failure)\;(pp)" "EDF\;(pp)"  "B/M" "Size" "I/K " "ROE\;(pp)" "Rating" /// 
                           "Book Leverage" "Leverage Ratio"  "WW Index" "SA Index" "\Delta CAPX/Asset" "\Delta ACQU/Asset" "GP\;(pp)" "IA\;(pp)" "ROA\;(pp)" "Reversal\;(pp)" "CF\;(pp)" "Profit\;(pp)" ///				  				   
						 

		
preserve

duplicates drop gvkey fyearq, force	
					   
local i = 1
foreach var of varlist strict_5r p_failure edf bm {
    
	* all observations
	quietly summarize `var',  detail
	mat panel_a[`i',1] = r(mean)
	mat panel_a[`i',2] = r(sd)
	mat panel_a[`i',3] = r(p25)
	mat panel_a[`i',4] = r(p50)
	mat panel_a[`i',5] = r(p75)
	mat panel_a[`i',6] = r(N)
	
	local i = `i' + 1	

}

restore

// size and reversal are monthly data
local i = 5
foreach var of varlist size {
    
	* all observations
	quietly summarize `var',  detail
	mat panel_a[`i',1] = r(mean)
	mat panel_a[`i',2] = r(sd)
	mat panel_a[`i',3] = r(p25)
	mat panel_a[`i',4] = r(p50)
	mat panel_a[`i',5] = r(p75)
	mat panel_a[`i',6] = r(N)
	
	local i = `i' + 1	

}
*******
preserve

duplicates drop gvkey fyearq, force	
					   
local i = 6
foreach var of varlist i_k roe rating book_lev lev_ratio  {
    
	* all observations
	quietly summarize `var',  detail
	mat panel_a[`i',1] = r(mean)
	mat panel_a[`i',2] = r(sd)
	mat panel_a[`i',3] = r(p25)
	mat panel_a[`i',4] = r(p50)
	mat panel_a[`i',5] = r(p75)
	mat panel_a[`i',6] = r(N)
	
	local i = `i' + 1	

}

restore

// financial constraint measures sa and ww are yearly data
preserve

duplicates drop gvkey fyear, force
// SA and WW have annual frequency
local i = 11
foreach var of varlist sa ww {
    
	* all observations
	quietly summarize `var',  detail
	mat panel_a[`i',1] = r(mean)
	mat panel_a[`i',2] = r(sd)
	mat panel_a[`i',3] = r(p25)
	mat panel_a[`i',4] = r(p50)
	mat panel_a[`i',5] = r(p75)
	mat panel_a[`i',6] = r(N)
	
	local i = `i' + 1	

}

restore
		
// following variables are quarterly data tangibility roa 
preserve

duplicates drop gvkey fyearq, force	
					   
local i = 13
foreach var of varlist diff4_capxy_asset diff4_aqcyq_asset  {
    
	* all observations
	quietly summarize `var',  detail
	mat panel_a[`i',1] = r(mean)
	mat panel_a[`i',2] = r(sd)
	mat panel_a[`i',3] = r(p25)
	mat panel_a[`i',4] = r(p50)
	mat panel_a[`i',5] = r(p75)
	mat panel_a[`i',6] = r(N)
	
	local i = `i' + 1	

}

restore		
		
		
// financial constraint measures sa and ww are yearly data
preserve

duplicates drop gvkey fyear, force
local i = 15
foreach var of varlist gp ia {
    
	* all observations
	quietly summarize `var',  detail
	mat panel_a[`i',1] = r(mean)
	mat panel_a[`i',2] = r(sd)
	mat panel_a[`i',3] = r(p25)
	mat panel_a[`i',4] = r(p50)
	mat panel_a[`i',5] = r(p75)
	mat panel_a[`i',6] = r(N)
	
	local i = `i' + 1	

}
restore
		
	
// following variables are quarterly data
preserve

duplicates drop gvkey fyearq, force	
					   
local i = 17
foreach var of varlist roa {
    
	* all observations
	quietly summarize `var',  detail
	mat panel_a[`i',1] = r(mean)
	mat panel_a[`i',2] = r(sd)
	mat panel_a[`i',3] = r(p25)
	mat panel_a[`i',4] = r(p50)
	mat panel_a[`i',5] = r(p75)
	mat panel_a[`i',6] = r(N)
	
	local i = `i' + 1	

}

restore
		
	
// size and reversal are monthly data
local i = 18
foreach var of varlist reversal {
    
	* all observations
	quietly summarize `var',  detail
	mat panel_a[`i',1] = r(mean)
	mat panel_a[`i',2] = r(sd)
	mat panel_a[`i',3] = r(p25)
	mat panel_a[`i',4] = r(p50)
	mat panel_a[`i',5] = r(p75)
	mat panel_a[`i',6] = r(N)
	
	local i = `i' + 1	

}

// following variables are quarterly data
preserve

duplicates drop gvkey fyearq, force	
					   
local i = 19
foreach var of varlist cf_asset profitability{
    
	* all observations
	quietly summarize `var',  detail
	mat panel_a[`i',1] = r(mean)
	mat panel_a[`i',2] = r(sd)
	mat panel_a[`i',3] = r(p25)
	mat panel_a[`i',4] = r(p50)
	mat panel_a[`i',5] = r(p75)
	mat panel_a[`i',6] = r(N)
	
	local i = `i' + 1	

}

restore



* output table
esttab matrix(panel_a, fmt(2 2 2 2 2 %15.0fc)), nomtitles ///
compress replace

esttab matrix(panel_a, fmt(2 2 2 2 2 %16.0fc)) using Output\table4.tex, nomtitles ///
booktabs replace fragment



////////////////////////////////////////////////////////////////////////////////
// Table 5: Characteristics of Strictness-Based Portfolios
////////////////////////////////////////////////////////////////////////////////

use Pseudodata/5port_firm_characteristics,clear
order port_5 exret strict_5r

* variables expressed in percentage points in the paper
foreach var of varlist strict_5r p_failure edf roe {
	replace `var' = `var'*100 // nim in basis points
}

*
// we can change the oder here
order strict_5r exret p_failure edf bm log_me i_k roe div_payout rating book_lev lev_ratio sa ww diff4_capxy_asset diff4_aqcyq_asset ia gp next_vio num_firms // std sr num_firms o z

xpose, clear varname format
order _varname
drop if _varname == "port_5"

// we can replace the variable names here
replace _varname = "Excess\;Return\;(pp)" if _varname == "exret"
replace _varname = "Strictness\;(pp)" if _varname == "strict_5r"
replace _varname = "B/M" if _varname == "bm"
replace _varname = "I/K" if _varname == "i_k"
replace _varname = "ROE\;(pp)" if _varname == "roe"
replace _varname = "SA\;Index" if _varname == "sa" 
replace _varname = "WW\;Index" if _varname == "ww" 
replace _varname = "Dividend\;Payout" if _varname ==  "div_payout"
replace _varname = "Rating\;Score" if _varname == "rating" 
replace _varname = "EDF\;(pp)" if _varname == "edf" 
replace _varname = "Pr(Failure)\;(pp)" if _varname == "p_failure" 
replace _varname = "Size" if _varname == "log_me" 
replace _varname = "Book\;Leverage" if _varname == "book_lev" 
replace _varname = "Leverage\;Ratio" if _varname == "lev_ratio" 
replace _varname = "Change\;in\;CAPX/Average\;assets" if _varname == "diff4_capxy_asset"
replace _varname = "Change\;in\;acquisitions/Average\;assets" if _varname == "diff4_aqcyq_asset"
replace _varname = "CF\;(pp)" if _varname == "cf_asset"  
replace _varname = "IA\;(pp)" if _varname == "ia"
replace _varname = "GP\;(pp)" if _varname == "gp"

replace _varname = "Average\;Number\;of\;Firms" if _varname == "num_firms" 

mkmat v1 v2 v3 v4 v5,  matrix(port_cha) rownames(_varname)  

matrix  colnames port_cha = "Low" "2" "3" "4" "High"
mat list port_cha

esttab matrix(port_cha, fmt(2 2 2 2 2 )), nomtitles ///
compress replace

esttab matrix(port_cha, fmt(2 2 2 2 2 )) using Output/table5.tex, nomtitles ///
booktabs replace fragment 


////////////////////////////////////////////////////////////////////////////////
// Table 6: Portfolios Sorted on Strictness
////////////////////////////////////////////////////////////////////////////////
use Pseudodata/Csave_5ff_whole, clear
sort year month
merge 1:1 year month using Pseudodata/hxz_egfactor
gen time = _n
tsset time


/*Annualized Excess Returns*/
gen exret_1 = exretadj_1*12
gen exret_2 = exretadj_2*12
gen exret_3 = exretadj_3*12
gen exret_4 = exretadj_4*12
gen exret_5 = exretadj_5*12
gen exret_6 = exretadj_6*12 // 
gen exret_7 = exretadj_7*12
gen exret_8 = exretadj_8*12


/*Annualized FF5*/
gen mkt_ann = mkt_rf*12
gen smb_ann = smb*12
gen hml_ann = hml*12
gen rmw_ann = rmw*12
gen cma_ann = cma*12

/*Annualized momentum*/
gen mom_ann = mom*12

/*Annualized HXZ*/
gen mkt_ann_hxz = mkt*12
gen me_ann = me*12
gen ia_ann = ia*12
gen roe_ann = roe*12
gen eg_ann = r_eg*12

****************
* Panel: A
****************

* Full sample summary statistics ***********************************************
matrix panel_b1 = J(2, 8,.)

// matrix  colnames panel_b = "1" "2" "3" "4" "5" "4-5" "5-1"
matrix  rownames panel_b1 = "Excess Return (pp)" "{\it t-}stat."   

forvalue i = 1/8{

  /*Excess Return*/
  newey exret_`i', lag(12)
  mat r_`i' = r(table)
  mat panel_b1[1,`i'] =_b[_cons]
  mat panel_b1[2,`i'] = r_`i'[3,1]
}

matrix  colnames panel_b1 = "Low" "2" "3" "4" "High" "High-4" "High-Low" "4-Low" 
mat list panel_b1

* output table
esttab matrix(panel_b1, fmt(2 2 2 2 2 2 2 2)), nomtitles ///
compress replace
esttab matrix(panel_b1, fmt(2 2 2 2 2 2 2 2)) using Output/table6_PanelA.tex, nomtitles ///
booktabs replace fragment 

****************************************
* Panel: B FF5 factor regression
****************************************

/*FF5*/
matrix panel_b2 = J(12, 8,.)
matrix  rownames panel_b2 = "$\alpha^{FF5}$" "{\it t-}stat." "$\beta^{MKT}$" "{\it t-}stat."  "$\beta^{SMB}$" "{\it t-}stat."  "$\beta^{HML}$" "{\it t-}stat."  ///
                            "$\beta^{RMW}$" "{\it t-}stat."  "$\beta^{CMA}$" "{\it t-}stat."

forvalue i = 1/8{
newey exret_`i' mkt_ann smb_ann hml_ann rmw_ann cma_ann, lag(12)
  mat r_`i' = r(table)
  mat panel_b2[1,`i'] =_b[_cons]
  // make sure to pick out the t statistics for constant term
  mat panel_b2[2,`i'] = r_`i'[3,6]
  
  mat panel_b2[3,`i'] =r_`i'[1,1]
  // make sure to pick out the t statistics for constant term
  mat panel_b2[4,`i'] = r_`i'[3,1]
  
  mat panel_b2[5,`i'] =r_`i'[1,2]
  // make sure to pick out the t statistics for constant term
  mat panel_b2[6,`i'] = r_`i'[3,2]
  
  mat panel_b2[7,`i'] =r_`i'[1,3]
  // make sure to pick out the t statistics for constant term
  mat panel_b2[8,`i'] = r_`i'[3,3]
  
  mat panel_b2[9,`i'] =r_`i'[1,4]
  // make sure to pick out the t statistics for constant term
  mat panel_b2[10,`i'] = r_`i'[3,4]
  
  mat panel_b2[11,`i'] =r_`i'[1,5]
  // make sure to pick out the t statistics for constant term
  mat panel_b2[12,`i'] = r_`i'[3,5]
  
  
}
*

matrix  colnames panel_b2 = "Low" "2" "3" "4" "High" "High-4" "High-Low" "4-Low" 
mat list panel_b2

* output table
esttab matrix(panel_b2, fmt(2 2 2 2 2 2 2 2)), nomtitles ///
compress replace
esttab matrix(panel_b2, fmt(2 2 2 2 2 2 2 2)) using Output/table6_PanelB.tex, nomtitles ///
booktabs replace fragment 

*****************************************
* Panel: C HXZ Q5 factor regression
*****************************************

est clear

// price of risk
egen mean_MKT = mean(mkt_ann_hxz)
egen mean_ME = mean(me_ann)
egen mean_IA = mean(ia_ann)
egen mean_ROE = mean(roe_ann)
egen mean_EG = mean(eg_ann)


* Full sample summary statistics ***********************************************
matrix panel_b = J(14, 8,.)

// matrix  colnames panel_b = "1" "2" "3" "4" "5" "4-5" "5-1"
matrix  rownames panel_b = "Excess Return (pp)" "{\it t-}stat." "$\alpha^{q5}$" "{\it t-}stat."  "$\beta^{MKT}\times\lambda^{MKT}$" "{\it t-}stat."  "$\beta^{ME}\times\lambda^{ME}$" "{\it t-}stat."  "$\beta^{I/A}\times\lambda^{I/A}$" "{\it t-}stat."  "$\beta^{ROE}\times\lambda^{ROE}$" "{\it t-}stat." "$\beta^{EG}\times\lambda^{EG}$" "{\it t-}stat."

 forvalue i = 1/8{

  /*Excess Return*/
  newey exret_`i', lag(12)
  mat r_`i' = r(table)
  mat panel_b[1,`i'] =_b[_cons]
  mat panel_b[2,`i'] = r_`i'[3,1]
}

*


/*HXZ*/
forvalue i = 1/8{
newey exret_`i' mkt_ann_hxz me_ann ia_ann roe_ann eg_ann, lag(12)
  mat r_`i' = r(table)
  mat panel_b[3,`i'] =_b[_cons]
   // make sure to pick out the t statistics for constant term
  mat panel_b[4,`i'] = r_`i'[3,6]
  
  mat panel_b[5,`i'] = r_`i'[1,1]*7.16
   // make sure to pick out the t statistics for constant term
  mat panel_b[6,`i'] = r_`i'[3,1]
  
    
  mat panel_b[7,`i'] = r_`i'[1,2]*3.60
   // make sure to pick out the t statistics for constant term
  mat panel_b[8,`i'] = r_`i'[3,2]
  
  mat panel_b[9,`i'] = r_`i'[1,3]*2.54
   // make sure to pick out the t statistics for constant term
  mat panel_b[10,`i'] = r_`i'[3,3]
  
  mat panel_b[11,`i'] = r_`i'[1,4]*4.04
   // make sure to pick out the t statistics for constant term
  mat panel_b[12,`i'] = r_`i'[3,4]
  
  mat panel_b[13,`i'] = r_`i'[1,5]*6.60
   // make sure to pick out the t statistics for constant term
  mat panel_b[14,`i'] = r_`i'[3,5]
  
  
}
*
matrix  colnames panel_b = "Low" "2" "3" "4" "High" "High-4" "High-Low" "4-Low"  
mat list panel_b

* output table
esttab matrix(panel_b, fmt(2 2 2 2 2 2 2 2)), nomtitles ///
compress replace
esttab matrix(panel_b, fmt(2 2 2 2 2 2 2 2)) using Output/table6_PanelC.tex, nomtitles ///
booktabs replace fragment 


////////////////////////////////////////////////////////////////////////////////
// Table 7: Fama-MacBeth Regressions on Strictness
////////////////////////////////////////////////////////////////////////////////

use Pseudodata/ccm41a,clear

*****************************************
* Fama-MacBeth regression 
*****************************************
label var strict_5r Strictness
label var  size Size
label var log_bm "Log B/M"
label var reversal Reversal
label var book_lev "Book Leverage"
label var roa "ROA"
label var p_failure "Pr(Failure)"
label var edf "EDF"

sort yearm permno
xtset permno yearm
// Fama-MacBeth regression with control
est clear
//eststo: quietly asreg exretadj strict_5r, fmb newey(6)
eststo: quietly asreg exretadj strict_5r size log_bm reversal book_lev roa, fmb newey(6) 
eststo: quietly asreg exretadj strict_5r size log_bm reversal book_lev roa p_failure, fmb newey(6) 
eststo: quietly asreg exretadj strict_5r size log_bm reversal book_lev roa edf, fmb newey(6) 
eststo: quietly asreg exretadj strict_5r size log_bm reversal book_lev roa p_failure edf, fmb newey(6) 


esttab,  se(%3.2f) b(3) stats(fixed_t r2 N, fmt(0 3 %8.0fc) label("R-Squared" "Observations")) se label /// statistics specs
compress /// table layout specs
prefoot("")  title(Main Results \label{maintbl}) star(* 0.1 ** 0.05 *** 0.01) /// table layout specs 
note("***, ** and * denote statistical significance at the 1%, 5% and 10% levels, respectively. Standard errors clustered at the BHC-level") nolegend /// note
replace // file specs
esttab using Output/table7, se(%3.2f) b(3) stats(r2 N, fmt(3 %13.0fc) label("R-Squared" "Observations")) se /// statistics specs
mgroups("Dependent Variable: Monthly Excess Returns", pattern(1 0 0 0 ) ///
span erepeat(\cmidrule(lr){@span}) prefix(\multicolumn{@span}{c}{) suffix(})) ///
prefoot("") star(* 0.1 ** 0.05 *** 0.01) nonotes nolegend nomtitles nocons label /// table layout specs 
fragment replace booktabs //file specs
estfe, restore


////////////////////////////////////////////////////////////////////////////////
// Table: 8 RDD regressions
////////////////////////////////////////////////////////////////////////////////

use Pseudodata/rdd_data,clear

gen violation_debteb = .
replace violation_debteb = 1 if (Debt_to_EBITDA > max_debt_to_ebitda & Debt_to_EBITDA ~= . & max_debt_to_ebitda ~= .)
replace violation_debteb = 0 if (Debt_to_EBITDA <= max_debt_to_ebitda & Debt_to_EBITDA ~= . & max_debt_to_ebitda ~= .)

gen gap_debteb = Debt_to_EBITDA-max_debt_to_ebitda
gen dum_gap_int = violation_debteb*gap_debteb
gen gap_debteb2 = gap_debteb*gap_debteb
gen dum_gap_int2 = violation_debteb*gap_debteb2
gen gap_debteb3 = gap_debteb*gap_debteb*gap_debteb
gen dum_gap_int3 = violation_debteb*gap_debteb3


drop if violation_debteb == .
sort gvkey yearq
bysort gvkey yearq: egen mean_exretadj = mean(exretadj)
duplicates drop gvkey yearq, force
drop exretadj
rename mean_exretadj exretadj


label var violation_debteb "Violation"
label var gap_debteb "Distance"
label var dum_gap_int "Violation $\times$ Distance"
label var size "Size"
label var log_bm "Log B/M"
label var book_lev "Book Leverage"
label var roa "ROA"
label var gap_debteb2 "Distance$^2$"
label var dum_gap_int2 "Violation $\times$ Distance$^2$"
label var gap_debteb3 "Distance$^3$"
label var dum_gap_int3 "Violation $\times$ Distance$^3$"


global depqtr "exretadj"  // dependent varialbe in monthly regressions		
		
********************************************************************************
* Use quarterly average return 
********************************************************************************

est clear

eststo: areg exretadj violation_debteb gap_debteb dum_gap_int, absorb(yearq) vce(cluster gvkey)
eststo: areg exretadj violation_debteb gap_debteb dum_gap_int size log_bm book_lev roa, absorb(yearq) vce(cluster gvkey)
eststo: areg exretadj violation_debteb gap_debteb gap_debteb2 dum_gap_int dum_gap_int2 size log_bm book_lev roa, absorb(yearq) vce(cluster gvkey)

esttab,  se(%3.2f) b(3) stats(fixed_t r2 N, fmt(0 3 %8.0fc) label("FE" "R-Squared" "Observations")) se label /// statistics specs
compress /// table layout specs
prefoot("")  title(Main Results \label{maintbl}) star(* 0.1 ** 0.05 *** 0.01) /// table layout specs 
note("***, ** and * denote statistical significance at the 1%, 5% and 10% levels, respectively. ") nolegend /// note
replace // file specs
esttab using Output/table8.tex, se(%3.2f) b(3) stats(r2 N, fmt(3 %13.0fc) label("R-Squared" "Observations")) se /// statistics specs
mgroups("Dependent Variable: Quarterly Returns", pattern(1 0 0 ) ///
span erepeat(\cmidrule(lr){@span}) prefix(\multicolumn{@span}{c}{) suffix(})) ///
prefoot("") star(* 0.1 ** 0.05 *** 0.01) nonotes nolegend nomtitles nocons label /// table layout specs 
fragment replace booktabs //file specs
estfe, restore


////////////////////////////////////////////////////////////////////////////////
//Table: 9 Portfolio-level Risk Exposures 
////////////////////////////////////////////////////////////////////////////////

use Pseudodata/Price_Risk, clear
merge 1:1 year qtr using Pseudodata/macro_indicator //nipa_qtr
keep if _merge ==3
drop _merge


drop exret_6
gen exret_6 = exret_5 - exret_4
gen exret_7 = exret_5 - exret_1
gen exret_8 = exret_4 -exret_1

sort yearq
gen num = _n

tsset num

egen gdp_nipa_std = std(gdpgrowth)
egen mkt_std = std(mkt)
egen pce_growth_std = std(pce_growth) 


//winsor2 exret_6 exret_7 exret_8,cuts(1,99) replace

est clear

* Full sample summary statistics ***********************************************
matrix panel_b = J(6, 8,.)

// matrix  colnames panel_b = "1" "2" "3" "4" "5" "4-5" "5-1"
matrix  rownames panel_b = "Constant" "{\it t-}stat." "MKT" "{\it t-}stat."  "dY" "{\it t-}stat." 


forvalue i = 1/8{

  newey exret_`i' mkt_std gdp_nipa_std, lag(36)
  mat r_`i' = r(table)
  mat panel_b[1,`i'] =_b[_cons]
   // make sure to pick out the t statistics for constant term
  mat panel_b[2,`i'] = r_`i'[3,3]
  
  mat panel_b[3,`i'] = r_`i'[1,1]
   // make sure to pick out the t statistics for constant term
  mat panel_b[4,`i'] = r_`i'[3,1]
  
    
  mat panel_b[5,`i'] = r_`i'[1,2]
   // make sure to pick out the t statistics for constant term
  mat panel_b[6,`i'] = r_`i'[3,2]
  
  
}
*
matrix  colnames panel_b = "Low" "2" "3" "4" "High" "High-4" "High-Low" "4-Low"  
mat list panel_b

* output table
esttab matrix(panel_b, fmt(2 2 2 2 2 2 2 2)), nomtitles ///
compress replace
esttab matrix(panel_b, fmt(2 2 2 2 2 2 2 2)) using Output/table9_PanelA.tex, nomtitles ///
booktabs replace fragment 


///////////////////////////
// For pce shock
///////////////////////////

est clear

matrix panel_c = J(6, 8,.)

// matrix  colnames panel_b = "1" "2" "3" "4" "5" "4-5" "5-1"
matrix  rownames panel_c = "Constant" "{\it t-}stat." "MKT" "{\it t-}stat."  "PCE" "{\it t-}stat." 

forvalue i = 1/8{

  // For PCE shock
  newey exret_`i' mkt_std pce_growth_std, lag(36)
  mat r_`i' = r(table)
  mat panel_c[1,`i'] =_b[_cons]
   // make sure to pick out the t statistics for constant term
  mat panel_c[2,`i'] = r_`i'[3,3]
  
  mat panel_c[3,`i'] = r_`i'[1,1]
   // make sure to pick out the t statistics for constant term
  mat panel_c[4,`i'] = r_`i'[3,1]
  
    
  mat panel_c[5,`i'] = r_`i'[1,2]
   // make sure to pick out the t statistics for constant term
  mat panel_c[6,`i'] = r_`i'[3,2]
  
  
}
*

matrix  colnames panel_c = "Low" "2" "3" "4" "High" "High-4" "High-Low" "4-Low"  
mat list panel_c

* output table
esttab matrix(panel_c, fmt(2 2 2 2 2 2 2 2)), nomtitles ///
compress replace
esttab matrix(panel_b, fmt(2 2 2 2 2 2 2 2)) using Output/table9_PanelB.tex, nomtitles ///
booktabs replace fragment 


////////////////////////////////////////////////////////////////////////////////
// Table 10: Estimating the price of risk
////////////////////////////////////////////////////////////////////////////////
use Pseudodata/Price_Risk, clear
merge 1:1 year qtr using Pseudodata/EGportfolios
keep if _merge ==3
drop _merge

merge 1:1 year qtr using Pseudodata/macro_indicator
keep if _merge ==3
drop _merge

drop exret_6
gen exret_6 = exret_5 - exret_1

sort yearq
gen num = _n

tsset num

egen gdp_nipa_std = std(gdpgrowth)
egen mkt_std = std(mkt)
egen pce_growth_std = std(pce_growth) 

// Define a matrix to store the results
matrix panel_c = J(10, 3,.)

// matrix  colnames panel_b = "1" "2" "3" "4" "5" "4-5" "5-1"
matrix  rownames panel_c = "MKT" "{\it t-}stat." "\Delta GDP" "{\it t-}stat." "\Delta PCE" "{\it t-}stat." "SSQE(\%)" "MAPE(\%)" "{\it J-}stat." "{\it p-}value"


/*I standardize all shocks with a zero mean and one-standard-deviation*/
/*shock1 is the aggregate shock*/
egen shock1 = std(mkt) 


**************************
* Column 1 of Table 10
**************************
set more off
gmm ///
(-exret_1*(1-{b1}*shock1)) ///
(-exret_2*(1-{b1}*shock1)) ///
(-exret_3*(1-{b1}*shock1)) ///
(-exret_4*(1-{b1}*shock1)) ///
(-exret_5*(1-{b1}*shock1)) ///
(-exret_6*(1-{b1}*shock1)) ///
(-ret_vw1*(1-{b1}*shock1)) ///
(-ret_vw2*(1-{b1}*shock1)) ///
(-ret_vw3*(1-{b1}*shock1)) ///
(-ret_vw4*(1-{b1}*shock1)) ///
(-ret_vw5*(1-{b1}*shock1)) ///
(-ret_vw6*(1-{b1}*shock1)), ///
winit(identity) twostep wmatrix(hac nw 3) 

//store the results
mat a1 = r(table)
mat panel_c[1,1] = a1[2,1]
mat panel_c[2,1] = a1[3,1]

//overidentificaiton test	
estat overid
mat panel_c[9,1] = r(J)
mat panel_c[10,1] = r(J_p)

qui gen b1 =_b[/b1]

qui gen g1 = -exret_1*0.01*(1-b1*shock1)
qui gen g2 = -exret_2*0.01*(1-b1*shock1)
qui gen g3 = -exret_3*0.01*(1-b1*shock1)
qui gen g4 = -exret_4*0.01*(1-b1*shock1)
qui gen g5 = -exret_5*0.01*(1-b1*shock1)
qui gen g6 = -exret_6*0.01*(1-b1*shock1)
qui gen g7 = -ret_vw1*0.01*(1-b1*shock1)
qui gen g8 = -ret_vw2*0.01*(1-b1*shock1)
qui gen g9 = -ret_vw3*0.01*(1-b1*shock1)
qui gen g10 = -ret_vw4*0.01*(1-b1*shock1)
qui gen g11 = -ret_vw5*0.01*(1-b1*shock1)
qui gen g12 = -ret_vw6*0.01*(1-b1*shock1)

qui egen double mean_1 = mean(g1)
qui egen double mean_2 = mean(g2)
qui egen double mean_3 = mean(g3)
qui egen double mean_4 = mean(g4)
qui egen double mean_5 = mean(g5)
qui egen double mean_6 = mean(g6)
qui egen double mean_7 = mean(g7)
qui egen double mean_8 = mean(g8)
qui egen double mean_9 = mean(g9)
qui egen double mean_10 = mean(g10)
qui egen double mean_11 = mean(g11)
qui egen double mean_12 = mean(g12)


qui gen ssqe = (mean_1*mean_1+mean_2*mean_2+mean_3*mean_3+mean_4*mean_4+mean_5*mean_5+mean_6*mean_6 /*
*/ +mean_7*mean_7+mean_8*mean_8+mean_9*mean_9+mean_10*mean_10+mean_11*mean_11+mean_12*mean_12)*100

qui gen mape = (( abs(mean_1)+abs(mean_2)+abs(mean_3)+abs(mean_4)+abs(mean_5)+abs(mean_6) /*
*/               +abs(mean_7)+abs(mean_8)+abs(mean_9)+abs(mean_10)+abs(mean_11)+abs(mean_12))/6)*100

qui keep ssqe mape
qui duplicates drop
set more off

sum ssqe
mat panel_c[7,1] = r(mean)

su mape
mat panel_c[8,1] = r(mean)



/////////////////////////////////////////////////////////////
// Column 2 of Table 10
////////////////////////////////////////////////////////////

use Pseudodata/Price_Risk, clear
merge 1:1 year qtr using Pseudodata/EGportfolios
keep if _merge ==3
drop _merge

merge 1:1 year qtr using Pseudodata/macro_indicator
keep if _merge ==3
drop _merge


drop exret_6
gen exret_6 = exret_5 - exret_1


sort yearq
gen num = _n

tsset num

egen gdp_nipa_std = std(gdpgrowth)
egen mkt_std = std(mkt)
egen pce_growth_std = std(pce_growth) 

/*I standardize all shocks with a zero mean and one-standard-deviation*/
/*shock1 is the aggregate shock*/
egen shock1 = std(mkt) 

gen shock2 = gdp_nipa_std

set more off
gmm ///
(-exret_1*(1-{b1}*shock1-{b2}*shock2)) ///
(-exret_2*(1-{b1}*shock1-{b2}*shock2)) ///
(-exret_3*(1-{b1}*shock1-{b2}*shock2)) ///
(-exret_4*(1-{b1}*shock1-{b2}*shock2)) ///
(-exret_5*(1-{b1}*shock1-{b2}*shock2)) ///
(-exret_6*(1-{b1}*shock1-{b2}*shock2)) ///
(-ret_vw1*(1-{b1}*shock1-{b2}*shock2)) ///
(-ret_vw2*(1-{b1}*shock1-{b2}*shock2)) ///
(-ret_vw3*(1-{b1}*shock1-{b2}*shock2)) ///
(-ret_vw4*(1-{b1}*shock1-{b2}*shock2)) ///
(-ret_vw5*(1-{b1}*shock1-{b2}*shock2)) ///
(-ret_vw6*(1-{b1}*shock1-{b2}*shock2)), ///
winit(identity) twostep wmatrix(hac nw 3) 

//store the results
mat a2 = r(table)
mat panel_c[1,2] = a2[1,1]
mat panel_c[2,2] = a2[3,1]
mat panel_c[3,2] = a2[1,2]
mat panel_c[4,2] = a2[3,2]

//overidentificaiton test	
estat overid
mat panel_c[9,2] = r(J)
mat panel_c[10,2] = r(J_p)
	

qui gen b1 =_b[/b1]
qui gen b2 =_b[/b2]

qui gen g1 = -exret_1*0.01*(1-b1*shock1-b2*shock2)
qui gen g2 = -exret_2*0.01*(1-b1*shock1-b2*shock2)
qui gen g3 = -exret_3*0.01*(1-b1*shock1-b2*shock2)
qui gen g4 = -exret_4*0.01*(1-b1*shock1-b2*shock2)
qui gen g5 = -exret_5*0.01*(1-b1*shock1-b2*shock2)
qui gen g6 = -exret_6*0.01*(1-b1*shock1-b2*shock2)
qui gen g7 = -ret_vw1*0.01*(1-b1*shock1-b2*shock2)
qui gen g8 = -ret_vw2*0.01*(1-b1*shock1-b2*shock2)
qui gen g9 = -ret_vw3*0.01*(1-b1*shock1-b2*shock2)
qui gen g10 = -ret_vw4*0.01*(1-b1*shock1-b2*shock2)
qui gen g11 = -ret_vw5*0.01*(1-b1*shock1-b2*shock2)
qui gen g12 = -ret_vw6*0.01*(1-b1*shock1-b2*shock2)


qui egen double mean_1 = mean(g1)
qui egen double mean_2 = mean(g2)
qui egen double mean_3 = mean(g3)
qui egen double mean_4 = mean(g4)
qui egen double mean_5 = mean(g5)
qui egen double mean_6 = mean(g6)
qui egen double mean_7 = mean(g7)
qui egen double mean_8 = mean(g8)
qui egen double mean_9 = mean(g9)
qui egen double mean_10 = mean(g10)
qui egen double mean_11 = mean(g11)
qui egen double mean_12 = mean(g12)



qui gen ssqe = (mean_1*mean_1+mean_2*mean_2+mean_3*mean_3+mean_4*mean_4+mean_5*mean_5+mean_6*mean_6 /*
*/ +mean_7*mean_7+mean_8*mean_8+mean_9*mean_9+mean_10*mean_10+mean_11*mean_11+mean_12*mean_12)*100

qui gen mape = (( abs(mean_1)+abs(mean_2)+abs(mean_3)+abs(mean_4)+abs(mean_5)+abs(mean_6) /*
*/               +abs(mean_7)+abs(mean_8)+abs(mean_9)+abs(mean_10)+abs(mean_11)+abs(mean_12))/6)*100

qui keep ssqe mape
qui duplicates drop
set more off
sum ssqe
mat panel_c[7,2] = r(mean)

su mape
mat panel_c[8,2] = r(mean)



/////////////////////////////////
// Column 3 of Table 10
/////////////////////////////////

use Pseudodata/Price_Risk, clear
merge 1:1 year qtr using Pseudodata/EGportfolios
keep if _merge ==3
drop _merge


merge 1:1 year qtr using Pseudodata/macro_indicator
keep if _merge ==3
drop _merge

drop exret_6
gen exret_6 = exret_5 - exret_1


sort yearq
gen num = _n

tsset num

egen gdp_nipa_std = std(gdpgrowth)
egen mkt_std = std(mkt)
egen pce_growth_std = std(pce_growth) 

/*I standardize all shocks with a zero mean and one-standard-deviation*/
/*shock1 is the aggregate shock*/
egen shock1 = std(mkt) 

gen shock2 = pce_growth_std

set more off
gmm ///
(-exret_1*(1-{b1}*shock1-{b2}*shock2)) ///
(-exret_2*(1-{b1}*shock1-{b2}*shock2)) ///
(-exret_3*(1-{b1}*shock1-{b2}*shock2)) ///
(-exret_4*(1-{b1}*shock1-{b2}*shock2)) ///
(-exret_5*(1-{b1}*shock1-{b2}*shock2)) ///
(-exret_6*(1-{b1}*shock1-{b2}*shock2)) ///
(-ret_vw1*(1-{b1}*shock1-{b2}*shock2)) ///
(-ret_vw2*(1-{b1}*shock1-{b2}*shock2)) ///
(-ret_vw3*(1-{b1}*shock1-{b2}*shock2)) ///
(-ret_vw4*(1-{b1}*shock1-{b2}*shock2)) ///
(-ret_vw5*(1-{b1}*shock1-{b2}*shock2)) ///
(-ret_vw6*(1-{b1}*shock1-{b2}*shock2)), ///
winit(identity) twostep wmatrix(hac nw 3) 

//store the results
mat a3 = r(table)
mat panel_c[1,3] = a3[1,1]
mat panel_c[2,3] = a3[3,1]
mat panel_c[5,3] = a3[1,2]
mat panel_c[6,3] = a3[3,2]

//overidentificaiton test	
estat overid
mat panel_c[9,3] = r(J)
mat panel_c[10,3] = r(J_p)	

qui gen b1 =_b[/b1]
qui gen b2 =_b[/b2]

qui gen g1 = -exret_1*0.01*(1-b1*shock1-b2*shock2)
qui gen g2 = -exret_2*0.01*(1-b1*shock1-b2*shock2)
qui gen g3 = -exret_3*0.01*(1-b1*shock1-b2*shock2)
qui gen g4 = -exret_4*0.01*(1-b1*shock1-b2*shock2)
qui gen g5 = -exret_5*0.01*(1-b1*shock1-b2*shock2)
qui gen g6 = -exret_6*0.01*(1-b1*shock1-b2*shock2)
qui gen g7 = -ret_vw1*0.01*(1-b1*shock1-b2*shock2)
qui gen g8 = -ret_vw2*0.01*(1-b1*shock1-b2*shock2)
qui gen g9 = -ret_vw3*0.01*(1-b1*shock1-b2*shock2)
qui gen g10 = -ret_vw4*0.01*(1-b1*shock1-b2*shock2)
qui gen g11 = -ret_vw5*0.01*(1-b1*shock1-b2*shock2)
qui gen g12 = -ret_vw6*0.01*(1-b1*shock1-b2*shock2)


qui egen double mean_1 = mean(g1)
qui egen double mean_2 = mean(g2)
qui egen double mean_3 = mean(g3)
qui egen double mean_4 = mean(g4)
qui egen double mean_5 = mean(g5)
qui egen double mean_6 = mean(g6)
qui egen double mean_7 = mean(g7)
qui egen double mean_8 = mean(g8)
qui egen double mean_9 = mean(g9)
qui egen double mean_10 = mean(g10)
qui egen double mean_11 = mean(g11)
qui egen double mean_12 = mean(g12)


qui gen ssqe = (mean_1*mean_1+mean_2*mean_2+mean_3*mean_3+mean_4*mean_4+mean_5*mean_5+mean_6*mean_6 /*
*/ +mean_7*mean_7+mean_8*mean_8+mean_9*mean_9+mean_10*mean_10+mean_11*mean_11+mean_12*mean_12)*100

qui gen mape = (( abs(mean_1)+abs(mean_2)+abs(mean_3)+abs(mean_4)+abs(mean_5)+abs(mean_6) /*
*/               +abs(mean_7)+abs(mean_8)+abs(mean_9)+abs(mean_10)+abs(mean_11)+abs(mean_12))/6)*100

qui keep ssqe mape
qui duplicates drop
set more off
mat panel_c[7,3] = r(mean)

su mape
mat panel_c[8,3] = r(mean)

* output table
esttab matrix(panel_c, fmt(2 2 2 )), nomtitles ///
compress replace
esttab matrix(panel_c, fmt(2 2 2 )) using Output/table10.tex, nomtitles ///
booktabs replace fragment 




////////////////////////////////////////////////////////////////////////////////
//Table: 11 Investment, Profitability, and Cash Flow Sensitivity to Aggregate Risk
////////////////////////////////////////////////////////////////////////////////

use Pseudodata/ccm41a,clear
drop year qtr month
rename fyear year
rename fqtr qtr
gen yearq = yq(year,qtr) 
format yearq %tq	

gen log_exret = log(1+exretadj/100)
bysort permno yearq: egen exret_qtrsum = sum(log_exret)
gen exret_qtr = (exp(exret_qtrsum )-1)*100
duplicates drop gvkey yearq, force

merge m:1 year qtr using Pseudodata/macro_indicator
keep if _merge ==3
drop _merge

replace cf_asset = cf_asset*100
replace profitability = profitability*100

gen int_strict_shock = strict_5r*gdpgrowth
gen int_strict_pce_growth = strict_5r*pce_growth

label var strict_5r Strictness
label var size Size
label var log_bm "Log B/M"
label var book_lev "Book Lev."
label var profitability "Profit"
label var gdpgrowth "$\Delta$ GDP"
label var int_strict_shock "$\Delta$ GDP $\times$ Strictness"
label var pce_growth "$\Delta$ PCE"
label var int_strict_pce_growth "$\Delta$ PCE $\times$ Strictness"
label var cf_asset "CF"
label var capxy_atq "CAPX/AT"

////////////////////////////////////////////////////////////////////////////////
// Investment/profitability/cashflow sensitivity
////////////////////////////////////////////////////////////////////////////////
		
duplicates drop permno yearq,force
xtset permno yearq
sort permno yearq
bysort permno: gen lag_profitability = L1.profitability
bysort permno: gen lag_cf_asset=L1.cf_asset
bysort permno: gen lead_profitability = F1.profitability
bysort permno: gen lead_cf_asset = F1.cf_asset
bysort permno: gen lead_capxy_atq = F1.capxy_atq


est clear
eststo: reghdfe capxy_atq_plus1 capxy_atq strict_5r gdpgrowth int_strict_shock size book_lev log_bm profitability, absorb(gvkey) vce(cluster gvkey)
eststo: reghdfe capxy_atq_plus1 capxy_atq strict_5r pce_growth int_strict_pce_growth size book_lev log_bm profitability, absorb(gvkey) vce(cluster gvkey)
eststo: reghdfe lead_profitability profitability strict_5r gdpgrowth int_strict_shock size book_lev log_bm, absorb(gvkey) vce(cluster gvkey)
eststo: reghdfe lead_profitability profitability strict_5r pce_growth int_strict_pce_growth size book_lev log_bm, absorb(gvkey) vce(cluster gvkey)
eststo: reghdfe lead_cf_asset cf_asset strict_5r gdpgrowth int_strict_shock size book_lev log_bm profitability, absorb(gvkey) vce(cluster gvkey)
eststo: reghdfe lead_cf_asset cf_asset strict_5r pce_growth int_strict_pce_growth size book_lev log_bm profitability, absorb(gvkey) vce(cluster gvkey)

esttab,  se(%3.2f) b(3) stats(fixed_t r2 N, fmt(0 3 %8.0fc) label("R-Squared" "Observations")) se label /// statistics specs
compress /// table layout specs
prefoot("")  title(Main Results \label{maintbl}) star(* 0.1 ** 0.05 *** 0.01) /// table layout specs 
note("***, ** and * denote statistical significance at the 1%, 5% and 10% levels, respectively. Standard errors clustered at the firm level") nolegend /// note
replace // file specs
esttab,  t(%5.3f) b(3) stats(fixed_t r2 N, fmt(0 3 %8.0fc) label("R-Squared" "Observations")) t label /// statistics specs
compress /// table layout specs
prefoot("")  title(Main Results \label{maintbl}) star(* 0.1 ** 0.05 *** 0.01) /// table layout specs 
note("***, ** and * denote statistical significance at the 1%, 5% and 10% levels, respectively. Standard errors clustered at the firm level") nolegend /// note
replace // file specs
esttab using Output/table11, se(%3.2f) b(3) stats(r2 N, fmt(3 %13.0fc) label("R-Squared" "Observations")) se /// statistics specs
mgroups("CAPX/AT$\_{t+1}$" "Profit$\_{t+1}$" "CF$\_{t+1}$", pattern(1 0 1 0 1 0) ///
span erepeat(\cmidrule(lr){@span}) prefix(\multicolumn{@span}{c}{) suffix(})) ///
prefoot("") star(* 0.1 ** 0.05 *** 0.01) nonotes nolegend nomtitles nocons label /// table layout specs 
fragment replace booktabs //file specs
estfe, restore


//////////////////////////////////////////////////////
// Table 12: Cyclical Pattern of Covenant Thresholds
//////////////////////////////////////////////////////

// merger everything together
use Pseudodata/deallink,clear
gen year = year(dealactivedate)
gen quarter = quarter(dealactivedate)
gen yearq=yq(year,quarter)
format yearq %tq
// consider data between 1996q1 - 2016q4
drop if year < 1996
drop if year > 2016
// for those five covenants we use
//  1) maximum debt to EBITDA, 
//  2) minimum interest coverage (EBITDA to interest expense), 
//  3) minimum fixed charge coverage, 
//  4) maximum leverage (long-term and current debt to total assets),
//  5) minimum current ratio (current assets to current liabilities).

rename initialratio2 max_debt_ebitda
rename initialratio4 min_interest_coverage
rename initialratio3 min_fixed_charge
rename initialratio12 max_leverage
rename initialratio7 min_current

duplicates drop gvkey packageid , force

replace max_debt_ebitda = 1/max_debt_ebitda
replace max_leverage    = 1/max_leverage

rename max_debt_ebitda r1
rename min_interest_coverage r2
rename min_fixed_charge r3
rename max_leverage r4
rename min_current r5

keep packageid gvkey r* loanamount yearq  maturity


reshape long r , i(gvkey packageid loanamount maturity yearq) j(covtype)
rename r cov_val

merge m:1 gvkey yearq using  Pseudodata/comp_ratios, keep(match) nogen //cleandata/comp or

// then merge the marco business cycle indicators

merge m:1 yearq using Pseudodata/macro_indicator, keep(match) nogen

joinby packageid using Pseudodata/package_lenders

foreach var of varlist cov_val DO* maturity bank*{
	winsor2 `var', replace
}
*

foreach i of numlist 1 2 3 4 5{
   preserve
   keep if covtype == `i'
   egen whole_mean = mean(cov_val)
   bysort sic: egen sic4mean = mean(cov_val)
   save Pseudodata/covtype_`i',replace
   restore
}

// append all  the data together
use Pseudodata/covtype_1,clear
append using Pseudodata/covtype_2
append using Pseudodata/covtype_3
append using Pseudodata/covtype_4
append using Pseudodata/covtype_5


gen cov_value_alt_sic4 = cov_val/sic4mean

fcollapse cov_val cov_value_alt* gdpgrowth DO* pce_growth sic4mean* atq loan* maturity bank* lag*, by(gvkey covtype yearq lcoid)
gen latq = log(atq)
gen lag_latq = log(lag_atq)
gen lloan = log(loanamount)
sort gvkey covtype yearq

gen log_cov_val = log(cov_val)
	
label var gdpgrowth "$\Delta$GDP"
label var pce_growth "$\Delta$PCE"
	
egen double_cluster = group(gvkey lcoid)

winsor2 cov_value_alt_sic4, replace by(covtype yearq)

// normalize LHS variable by cross-sectional mean in the whole sample
// For GDP
eststo clear
eststo : quietly reghdfe cov_value_alt_sic4 lag_gdpgrowth, absorb(gvkey lcoid) vce(cluster double_cluster)
eststo : quietly reghdfe cov_value_alt_sic4 lag_gdpgrowth DO_covvalue2 DO_covvalue3 DO_covvalue4 DO_covvalue7 DO_covvalue12 , absorb( gvkey lcoid) vce(cluster double_cluster)
eststo : quietly reghdfe cov_value_alt_sic4 lag_gdpgrowth DO_covvalue2 DO_covvalue3 DO_covvalue4 DO_covvalue7 DO_covvalue12 lloan maturity , absorb( gvkey lcoid) vce(cluster double_cluster)
eststo : quietly reghdfe cov_value_alt_sic4 lag_gdpgrowth DO_covvalue2 DO_covvalue3 DO_covvalue4 DO_covvalue7 DO_covvalue12 lloan maturity bank_tier1  bank_levg , absorb( gvkey lcoid) vce(cluster double_cluster)
	
eststo : quietly reghdfe cov_value_alt_sic4 lag_pce_growth,  absorb( gvkey lcoid) vce(cluster double_cluster)
eststo : quietly reghdfe cov_value_alt_sic4 lag_pce_growth DO_covvalue2 DO_covvalue3 DO_covvalue4 DO_covvalue7 DO_covvalue12 , absorb( gvkey lcoid) vce(cluster double_cluster)
eststo : quietly reghdfe cov_value_alt_sic4 lag_pce_growth DO_covvalue2 DO_covvalue3 DO_covvalue4 DO_covvalue7 DO_covvalue12 lloan maturity , absorb( gvkey lcoid) vce(cluster double_cluster)
eststo : quietly reghdfe cov_value_alt_sic4 lag_pce_growth DO_covvalue2 DO_covvalue3 DO_covvalue4 DO_covvalue7 DO_covvalue12 lloan maturity bank_tier1 bank_levg , absorb( gvkey lcoid) vce(cluster double_cluster)


esttab,  se(%3.2f) b(3) stats(fixed_t r2 N, fmt(0 3 %8.0fc) label("FE" "R-Squared" "Observations")) se label noconstant /// statistics specs
compress /// table layout specs
indicate("Firm-level Controls = DO*" "Loan-level Controls = lloan maturity" "Lender-level Controls = bank_tier1 bank_levg" ) ///
prefoot("")  title(Main Results \label{maintbl}) star(* 0.1 ** 0.05 *** 0.01) /// table layout specs 
note("***, ** and * denote statistical significance at the 1%, 5% and 10% levels, respectively. Standard errors clustered at the BHC-level") nolegend /// note
replace // 
esttab using Output/table12, se(%3.2f) b(3) stats(r2 N, fmt(3 %13.0fc) label("R-Squared" "Observations")) se /// statistics specs
indicate("Firm-level Controls = DO*" "Loan-level Controls = lloan maturity" "Lender-level Controls = bank_tier1 bank_levg" ) ///
mgroups("Dependent Variable: Covenant Thresholds", pattern(1 0 0 0 ) ///
span erepeat(\cmidrule(lr){@span}) prefix(\multicolumn{@span}{c}{) suffix(})) ///
prefoot("") star(* 0.1 ** 0.05 *** 0.01) nonotes nolegend nomtitles nocons label /// table layout specs 
fragment replace booktabs //file specs
estfe, restore 
